﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("UnitTests")]

namespace AccessKeyHighlighterControl
{
    /// <summary>
    /// Contains static helper methods for the AccessKeyHighlighter project
    /// </summary>
    internal static class Helpers
    {
        /// <summary>
        /// Walks the control collection of the page and injects mark-up for access keys into Label controls
        /// </summary>
        /// <param name="accessKeyHighlighter">The AccessKeyHighlighter</param>
        internal static void InjectAccessKeyMarkup(AccessKeyHighlighterControl.AccessKeyHighlighter accessKeyHighlighter)
        {
            InjectAccessKeyMarkup(accessKeyHighlighter, accessKeyHighlighter.Page.Controls);
        }

        /// <summary>
        /// Walks a control collection and injects mark-up for access keys into Label controls
        /// </summary>
        /// <param name="accessKeyHighlighter">The AccessKeyHighlighter</param>
        /// <param name="controls">The control collection</param>
        private static void InjectAccessKeyMarkup(AccessKeyHighlighterControl.AccessKeyHighlighter accessKeyHighlighter, ControlCollection controls)
        {
            Label lbl = null;
            foreach (Control ctl in controls)
            {
                lbl = ctl as Label;
                if (lbl != null)
                {
                    // Get the access key from the associated control
                    string accessKey = null;
                    if (!String.IsNullOrEmpty(lbl.AssociatedControlID))
                    {
                        WebControl wc = lbl.NamingContainer.FindControl(lbl.AssociatedControlID) as WebControl;
                        accessKey = (wc != null ? wc.AccessKey : null);
                    }
                    if (String.IsNullOrEmpty(accessKey))
                    {
                        accessKey = lbl.AccessKey;
                    }

                    if (String.IsNullOrEmpty(accessKey)) continue;

                    int accessKeyIndex = lbl.Text.IndexOf(accessKey, StringComparison.InvariantCultureIgnoreCase);
                    if (accessKeyIndex < 0) continue;

                    // Inject in the markup, e.g., "First Name:" -> "<em>F</em>irst Name:"
                    string tag = Enum.GetName(typeof(HtmlTextWriterTag), accessKeyHighlighter.AccessKeyTag).ToLower();
                    string accessKeyMarkup = String.Format("<{0}>{1}</{0}>", tag, lbl.Text.Substring(accessKeyIndex, 1));
                    string newLabelText = lbl.Text.Remove(accessKeyIndex, 1).Insert(accessKeyIndex, accessKeyMarkup);

                    if (lbl.Text.IndexOf(accessKeyMarkup, StringComparison.InvariantCultureIgnoreCase) < 0) lbl.Text = newLabelText;
                }
                else if (ctl.HasControls())
                {
                    InjectAccessKeyMarkup(accessKeyHighlighter, ctl.Controls);
                }
            }
        }
    }
}
